package com.example.demo.service.Impl;

import com.example.demo.bean.*;
import com.example.demo.dao.CategoryMapper;
import com.example.demo.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service

public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    public List<Category> query(Long id, String name) {
        CategoryExample example = new CategoryExample();
        if (!StringUtils.isEmpty(name)) {
            example.createCriteria().andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(id)) {
            example.createCriteria().andIdEqualTo(id);
        }
        List<Category> list = categoryMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Category category) {
        if(category.getId()==null){
            categoryMapper.insert(category);
        }else{
            categoryMapper.updateByPrimaryKey(category);
        }

    }

    @Override
    public void deleteById(long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }



}
